package cn.itcast.weatherapp123.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import cn.itcast.weatherapp123.R;
import cn.itcast.weatherapp123.bend.DayWeatherBend;

public class FutureWeatherAdapter extends RecyclerView.Adapter<FutureWeatherAdapter.WeatherViewHold> {

    private List<DayWeatherBend> dayWeatherBends;
    private Context mContext;


    public FutureWeatherAdapter(Context context,List<DayWeatherBend> mdayWeatherBends){

        this.mContext=context;
        this.dayWeatherBends=mdayWeatherBends;

    }


    @NonNull
    @Override
    public WeatherViewHold onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view=LayoutInflater.from(mContext).inflate(R.layout.weather_item_layout,parent,false);
        WeatherViewHold weatherViewHold=new WeatherViewHold(view);
        return weatherViewHold;

    }

    @Override
    public void onBindViewHolder(@NonNull WeatherViewHold holder, int position) {

        DayWeatherBend dayWeatherBend=dayWeatherBends.get(position);
        holder.tvWeather.setText(dayWeatherBend.getWea());
        holder.tvTem.setText(dayWeatherBend.getTem());
        holder.tvTemLowHigh.setText(dayWeatherBend.getTem2()+"-"+dayWeatherBend.getTem1());
        holder.tvWin.setText(dayWeatherBend.getWin()[0]+dayWeatherBend.getWinSpeed());
        holder.tvAir.setText("空气"+dayWeatherBend.getAir()+dayWeatherBend.getAirLevel());
        holder.tvDate.setText("("+dayWeatherBend.getDate()+dayWeatherBend.getWeek()+")");
        holder.ivWeather.setImageResource(getImResOfWeather(dayWeatherBend.getWeaImg()));
    }


    @Override
    public int getItemCount() {

        return (dayWeatherBends==null)?0:dayWeatherBends.size();

    }

    class WeatherViewHold extends RecyclerView.ViewHolder{

        private TextView tvWeather, tvTem, tvTemLowHigh, tvWin, tvAir,tvDate;
        private ImageView ivWeather;

        public WeatherViewHold(@NonNull View itemView) {
            super(itemView);

            tvWeather=itemView.findViewById(R.id.tv_mweather);
            tvAir=itemView.findViewById(R.id.tv_air);
            tvTem=itemView.findViewById(R.id.tv_tem);
            tvTemLowHigh=itemView.findViewById(R.id.tv_tem_low_high);
            tvWin=itemView.findViewById(R.id.tv_win);
            ivWeather=itemView.findViewById(R.id.iv_weather);
            tvDate=itemView.findViewById(R.id.tv_date);

        }
    }
    //xue、lei、shachen、wu、bingbao、yun、yu、yin、qing
    private int getImResOfWeather(String weaImg) {
        int result = 0;
        switch (weaImg) {
            case "qing":
                result = R.mipmap.qing;
                break;
            case "yin":
                result = R.mipmap.yin;
                break;
            case "yu":
                result = R.mipmap.yu;
                break;
            case "bingbao":
                result = R.mipmap.bingbao;
                break;
            case "wu":
                result = R.mipmap.wu;
                break;
            case "shachen":
                result = R.mipmap.shachen;
                break;
            case "lei":
                result = R.mipmap.lei;
                break;
            case "xue":
                result = R.mipmap.xue;
                break;
            case "yun":
                result=R.mipmap.yun;
                break;
        }
        return result;
    }

}
